import 'package:base_lib/common/base/base_bloc_page.dart';
import 'package:base_lib/common/index.dart';
import 'package:base_lib/index.dart';
import 'package:base_lib/router/routers.dart';
import 'package:base_lib/theme/theme_global.dart';
import 'package:base_lib/utils/screen_adapter.dart';
import 'package:base_lib/utils/toast.dart';
import 'package:doctor_app/app_component.dart';
import 'package:doctor_app/home_page/bloc.dart';
import 'package:doctor_examine_lib/doctor_examine_componet.dart';
import 'package:doctor_inquiry_lib/chat_list/chat_list_widget.dart';
import 'package:doctor_inquiry_lib/inquiry_plugin.dart';
import 'package:doctor_inquiry_lib/repository/im_http.dart';
import 'package:doctor_login_lib/cache/userinfo_cache.dart';
import 'package:doctor_login_lib/component.dart';
import 'package:doctor_prescribe_lib/prescribe_util.dart';
import 'package:doctor_user_lib/doctor_user_component.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';

class HomePage extends BaseBlocPage<HomeBloc, _HomePageState> {
  HomePage(Parameters parameters) : super(parameters);

  @override
  _HomePageState generateThemeState() {
    return _HomePageState();
  }
}

class _HomePageState extends BaseBlocPageState<HomeBloc> {
  var tsWhite = TextStyle(fontSize: 16, color: Colors.white);
  var tsBlackBig = TextStyle(fontSize: 16, color: Colors.black);
  var tsBlack = TextStyle(fontSize: 14, color: Colors.black);

  var menuList = <_MenuList>[];

  @override
  void initState() {
    super.initState();

    menuList
      ..clear()
      ..addAll([
        _MenuList('images/work_room/transfer.png', '转诊', () {
          ToastUtil.info('功能开发中');
        }),
        _MenuList('images/work_room/examine.png', '开检查单', () {
          Routers.navigateTo(
              context, DoctorExamineComponent.offlineExaminePage);
        }),
        _MenuList('images/work_room/prescription.png', '开处方', () {
          PrescribeUtil.showPrescribeDialog(context);
        }),
      ]);

    UserInfoCache.getInstance().get().then((value) {
      bloc.userInfo = value;
      ChatListWidget.doImLogin(bloc.userInfo.keyID);
      setState(() {});
    });
  }

  @override
  Widget buildChild(BuildContext context, MTheme theme) {
    return Scaffold(
      backgroundColor: theme.themeColor.themeBgColor,
      appBar: CustomAppbar(
        contentHeight: 0,
      ),
      body: Column(
        children: [
          Container(
            width: ScreenAdapter.screenWidthDp,
            child: Stack(
              alignment: Alignment(0, -1),
              children: [
                Container(
                  padding: EdgeInsets.all(10),
                  color: theme.themeColor.themeColor,
                  width: double.infinity,
                  height: ScreenAdapter.screenWidthDp * 0.6 * 0.5,
                  child: Text(
                    '我的工作室',
                    style: tsWhite.copyWith(fontSize: 20),
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(
                      top: ScreenAdapter.screenWidthDp * 0.6 * 0.5 * 0.5,
                      left: 20,
                      right: 20,
                      bottom: 20),
                  padding: EdgeInsets.all(20),
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(20)),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        mainAxisSize: MainAxisSize.max,
                        children: [
                          ClipRRect(
                            child: FadeInImage.assetNetwork(
                              placeholder: 'images/doctor.png',
                              image: bloc.userInfo?.faceImage ?? '',
                              height: 60,
                              width: 60,
                            ),
                            borderRadius: BorderRadius.circular(30),
                          ),
                          10.hGapDp,
                          Text(
                            bloc?.userInfo?.doctorName ?? '',
                            style: tsBlack,
                          ),
                          10.hGapDp,
                          GestureDetector(
                              child: Image.asset(
                                'images/work_room/qr_code.png',
                                width: 18,
                                height: 18,
                              ),
                              onTap: () {
                                Routers.navigateTo(
                                    context, AppComponent.qrCardPage);
                              }),
                        ],
                      ),
                      10.vGapDp,
                      DottedLine(
                        color: Colors.grey[200],
                        dashWidth: 2,
                      ),
                      10.vGapDp,
                      _buildMenu(theme)
                    ],
                  ),
                ),
              ],
            ),
          ),
          Expanded(
            child: Container(
              width: double.infinity,
              padding: EdgeInsets.all(10),
              color: Colors.white,
              child: ChatListWidget(),
            ),
          ),
          15.hGapDp,
        ],
      ),
    );
  }

  ///患者服务菜单
  _buildMenu(MTheme theme) {
    var widgets = <Widget>[];

    menuList.forEach((element) {
      var w = GestureDetector(
        onTap: element.function,
        child: Container(
          padding: EdgeInsets.all(10),
          child: Column(
            children: [
              Image.asset(
                element.image,
                height: 22,
                width: 22,
                fit: BoxFit.fill,
              ),
              10.hGapDp,
              Text(
                element.des,
                style: tsBlack.copyWith(fontSize: 12),
              )
            ],
          ),
        ),
      );

      widgets.add(w);
    });

    return Wrap(
      children: widgets,
    );
  }

  @override
  HomeBloc generateBloc() {
    return HomeBloc();
  }
}

class _MenuList {
  String image;
  String des;
  Function function;

  _MenuList(this.image, this.des, this.function);
}
